package com.bw.myapply_module.applycome

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.CompoundButton
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.bw.myapply_module.R
import com.bw.myapply_module.databinding.ActivityComeBinding
import com.bw.myapply_module.myapply.entity.ApplyEntity
import com.kf.common.mvi.view.BaseActivity
import com.kf.common.utils.DateUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Route(path = "/apply/applycomeactivity")
class ApplyComeActivity : BaseActivity<ActivityComeBinding, ApplyComeViewModel>(),
    View.OnClickListener {
    @Autowired
    lateinit var applyEntity: ApplyEntity
    var simpleDate = SimpleDateFormat("yyyy-MM-dd")

    override fun createViewModel(): ApplyComeViewModel {
        return viewModels<ApplyComeViewModel>().value
    }

    override fun createViewBind(): ActivityComeBinding {
        return ActivityComeBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        initView()
        BarUtils.setStatusBarColor(this, Color.parseColor("#2196F3"))
        mViewModel.requestApplyCome(applyEntity.visitorId)
        initBind()
        initClick()
    }

    private fun initView() {
        mView.visitorName.setText(SPUtils.getInstance().getString("userName", "hxw"))
    }

    private fun initClick() {
        mView.comeBack.setOnClickListener(this)
        mView.visitorSwitch.setOnCheckedChangeListener(object :CompoundButton.OnCheckedChangeListener{
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                if (isChecked)
                    mView.visitorLinear.visibility = View.VISIBLE
                else
                    mView.visitorLinear.visibility = View.GONE
            }
        })
    }

    private fun initBind() {
        lifecycleScope.launch(Dispatchers.Main) {
            mViewModel.mutableContainer
                .uiStateFlow
                .collect {
                    if (it?.comeEntity != null) {
                        if (it!!.comeEntity!!.statuesCode == 200) {
                            var applyComeEntity = it!!.comeEntity!!.values as ApplyComeEntity
                            LogUtils.e("applyComeEntity",applyComeEntity.toString())
                            mView.visitorEndTime.setText(DateUtils().dateUtils(applyComeEntity.visitorEnd).removeRange(0,4))
                            mView.visitorStartTime.setText(DateUtils().dateUtils(applyComeEntity.visitorStart).removeRange(0,4))
                            mView.visitorNumber.setText(applyComeEntity.carNum)
                            mView.visitorMessage.setText(applyComeEntity.visitorDescribe)
                            mView.visitorCar.setText("${applyComeEntity.parkId}号车位")
                            if (!TextUtils.isEmpty(applyComeEntity.visitorTime)){
                                mView.visitorData.setText(DateUtils().dateUtils(applyComeEntity.visitorTime).removeRange(0,4))
                            }else{
                                var currentTimeMillis = System.currentTimeMillis()
                                var format = simpleDate.format(currentTimeMillis)
                                mView.visitorData.setText(format)
                            }
                        }
                    }
                }
        }
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.come_back -> {
                finish()
            }
        }
    }
}